package com.comsoftstar.nt5s.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hzc.nt5s.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

/**
 * Created by Administrator on 2017/6/13.
 */

public class SearchDateAdapter extends BaseAdapter {

    private JSONArray jsonArray;
    private Context mContext;
    private Map<String,String> map;

    public SearchDateAdapter(Context context, JSONArray ja, Map<String,String> map) {
        this.jsonArray = ja;
        this.mContext = context;
        this.map=map;
    }
    public void refresh(JSONArray ja, Map<String,String> map) {
        this.jsonArray = ja;
        this.map=map;
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {

        return jsonArray.length();
    }

    @Override
    public Object getItem(int position) {
        try {
            return jsonArray.get(position);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        JSONObject jo = new JSONObject();
        try {
            jo = (JSONObject) jsonArray.get(position);
        } catch (JSONException e) {

        }
        if (convertView==null){
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item, null);
            viewHolder.ispass= (ImageView) convertView.findViewById(R.id.ispass);
            viewHolder.group= (TextView) convertView.findViewById(R.id.item_group);
            viewHolder.outdate= (TextView) convertView.findViewById(R.id.item_outdate);
            viewHolder.regulation= (TextView) convertView.findViewById(R.id.item_regulation);
            viewHolder.newcardnum= (TextView) convertView.findViewById(R.id.item_newcardnum);
            viewHolder.person= (TextView) convertView.findViewById(R.id.item_person);
            viewHolder.checkdate= (TextView) convertView.findViewById(R.id.item_checkdate);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (jo.optString("ISOK").equals("1")){
            viewHolder.ispass.setBackgroundResource(R.mipmap.icon_box_checked);
        }else {
            viewHolder.ispass.setBackgroundResource(R.mipmap.icon_box_empty);
        }

        viewHolder.group.setText(getKey(jo.optString("WorkShop")));
        viewHolder.outdate.setText(jo.optString("OutDate").split(" ")[0]);
        viewHolder.regulation.setText(jo.optString("Regulation"));
        viewHolder.newcardnum.setText(jo.optString("NewCardNum"));
        viewHolder.person.setText(jo.optString("Person"));
        viewHolder.checkdate.setText(jo.optString("CheckDate").split(" ")[0]);
        return convertView;
    }
    class ViewHolder {
        TextView group, outdate, regulation, newcardnum, person, checkdate;
        ImageView ispass;
    }
    public String getKey(String value){
        String key=new String();
        for(Map.Entry<String,String> entry:this.map.entrySet()){
            if(value.equals(entry.getValue())){
                key=entry.getKey();
            }else{
                continue;
            }
        }
        return key;
    }

}
